﻿using KAKA.Domain;
using KAKA.Repository;
using Morus;
using Morus.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace KAKA.Infrastructure
{
    public class RegisterTextMessageSender : IBootstrapperTask
    {
        private string _serialNumber;
        private string _password;
        private string _company;
        private string _contact;
        private string _tel;
        private string _mobile;
        private string _email;
        private string _fax;
        private string _address;
        private string _postalCode;

        public RegisterTextMessageSender(string serialNumber, string password, string company, string contact, string tel, string mobile, string email, string fax, string address, string postalCode)
        {
            Check.Argument.IsNotEmpty(serialNumber, "serialNumber");
            Check.Argument.IsNotEmpty(password, "password");

            _serialNumber = serialNumber;
            _password = password;
            _company = company;
            _contact = contact;
            _tel = tel;
            _mobile = mobile;
            _email = email;
            _fax = fax;
            _address = address;
            _postalCode = postalCode;
        }

        public void Execute()
        {
            var settingRepository = IoC.Resolve<ISettingRepository>();
            var setting = settingRepository.GetByName(Setting.NAME_REGISTRY_SMS_SERVICE_INITIALIZED);

            if (setting == null || Convert.ToBoolean(setting.Value) == false)
            {
                using (SMS.SDKService sms = new SMS.SDKService())
                {
                    sms.registDetailInfo(_serialNumber, _password, _company, _contact, _tel, _mobile, _email, _fax, _address, _postalCode);
                }

                settingRepository.Add(new Setting
                {
                    Id = Guid.NewGuid(),
                    Name = Setting.NAME_REGISTRY_SMS_SERVICE_INITIALIZED,
                    Value = true.ToString()
                });

                settingRepository.Commit();
            }
        }
    }
}